package mypackage3;

import java.util.Scanner;

public class HightPrecisionMultiplication {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入第一个数字");
        String num1=sc.next();
        System.out.println("请输入第二个数字");
        String num2=sc.next();
        boolean flag=init(num1,num2);
        if(num1.charAt(0)=='-'){
            num1=num1.substring(1);
        }
        if(num2.charAt(0)=='-'){
            num2=num2.substring(1);
        }
        //System.out.println(num1);
        //System.out.println(num2);
        int[] number1=new int[num1.length()];//存第一个数
        int[] number2=new int[num2.length()];//存第二个数
        //倒序存储
        for (int i = 0; i < number1.length; i++) {
            number1[i]=(int)(num1.charAt(number1.length-i-1)-'0');
        }
        for (int i = 0; i < number2.length; i++) {
            number2[i]=(int)(num2.charAt(number2.length-i-1)-'0');
        }
        //print(number1);
        //print(number2);
        StringBuilder sb=getnumberMultiplication(number1,number2,flag);
        System.out.println(sb);
    }
    public static boolean init(String num1,String num2){
        boolean flag=true;
        if(num1.charAt(0)=='-'&&num2.charAt(0)!='-'){
            flag=false;
        }
        if(num2.charAt(0)=='-'&&num1.charAt(0)!='-'){
            flag=false;
        }
        if((num1.charAt(0)=='0' && check(num1)==true) || (num2.charAt(0)=='0' && check(num2)==true)){
            flag=true;
        }
        return flag;
    }
    public static boolean check(String arr){
        //前导0的特判
        for (int i = 0; i < arr.length(); i++) {
            if(arr.charAt(i)!='0'){
                return false;
            }
        }
        return true;
    }
    public static void print(int[] arr){
        //输出翻转的数组数字
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
    public static StringBuilder getnumberMultiplication(int[] number1,int[] number2,boolean flag){
        int[] numberMultiplication=new int[number1.length+number2.length+1];//存乘积
        //乘法原理
        for (int i = 0; i < number1.length; i++) {
            for (int j = 0; j < number2.length; j++) {
                numberMultiplication[i+j]+=number1[i]*number2[j];
            }
        }
        for (int i = 0; i < numberMultiplication.length; i++) {
            if(numberMultiplication[i]>9){
                numberMultiplication[i+1]+=numberMultiplication[i]/10;
                numberMultiplication[i]%=10;
            }
        }
        int anslen=numberMultiplication.length-1;//计算实际长度
        while(anslen>0 && numberMultiplication[anslen]==0){
            anslen--;
        }
        StringBuilder sb=new StringBuilder();//存答案
        for (int i = anslen; i >= 0; i--) {
            char c=(char)(numberMultiplication[i]+'0');
            sb.append(c);
        }
        if(flag==false){
            sb.insert(0,'-');
        }
        return sb;
    }
}
